# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=creduce
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=2.10.0
pkgrel=1
pkgdesc="A C program reducer (mingw-w64)"
depends=('perl-Benchmark-Timer'
         'perl-Exporter-Lite'
         'perl-File-Which'
         'perl-Getopt-Tabular'
         'perl-Regexp-Common'
         'perl-Sys-CPU'
         "${MINGW_PACKAGE_PREFIX}-astyle"
         "${MINGW_PACKAGE_PREFIX}-indent"
         "${MINGW_PACKAGE_PREFIX}-clang")
arch=('any')
url='https://github.com/csmith-project/creduce'
license=('custom:University of Illinois/NCSA Open Source License')
makedepends=("flex" "${MINGW_PACKAGE_PREFIX}-clang")
source=(https://github.com/csmith-project/creduce/archive/${_realname}-${pkgver}.tar.gz
        "llvm-config-cygpath"
        "002-fix-linking.patch")
sha256sums=('de320cd83bd77ec1a591f36dd6a4d0d1c47a0a28d850a6ebd348540feeab2297'
            '03a7239d1e26d8a5dd5f405e10826679725cae6c01a9c37e80c6f1c184dad0a9'
            'd08dd0e6c8beb3c2c2b9f2aa32e265f22068f67a6c26b7c849bab6a704bc1d3b')

prepare() {
  cd ${srcdir}/${_realname}-${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/002-fix-linking.patch

  cd "${srcdir}"
  [[ -d tools ]] && rm -rf tools
  mkdir tools
  cp -f "${srcdir}"/llvm-config-cygpath tools/llvm-config
}

build() {
  cd "${srcdir}"
  [[ -d ${CARCH} ]] && rm -rf ${CARCH}
  mkdir ${CARCH} && cd ${CARCH}
  PATH="${srcdir}/tools:$PATH" \
  ../${_realname}-${_realname}-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST}

  make
}

package() {
  cd "${srcdir}/${CARCH}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "../${_realname}-${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"

  # Point creduce perl script:
  # #!/${MINGW_PREFIX} -> #!/usr
  # ..  to msys2 perl.
  sed "s|#!${MINGW_PREFIX}|#!/usr|g" \
    -i ${pkgdir}${MINGW_PREFIX}/bin/creduce
}
